const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    eIndex: 0,
    isMember:true,
    pageSize:10,
    pageNumber:1,
    orderStatuses:[1,2,3,4,5]
  },

  // 菜单切换类名on
  tab: function (e) {
    console.log(11)
    this.setData({
      eIndex: e.currentTarget.dataset.index
    })
    if (this.data.eIndex == 0) {
      this.setData({
        orderStatuses: [1, 2,3, 4, 5]
      });
    } else if (this.data.eIndex == 1) {
      this.setData({
        orderStatuses: [3]
      }); 
    } else if (this.data.eIndex == 2) {
      this.setData({
        orderStatuses: [4]
      });
    } else if (this.data.eIndex == 3) {
      this.setData({
        orderStatuses: [5]
      });
    } 
    this.setData({
      pageNumber: 1
    });
    this.loadOrder(this.data.userInfo.userType, this.data.userInfo.id, this.data.pageSize, this.data.pageNumber, this.data.orderStatuses)
  },

  // 跳转到订单详情
  toOrderDetails: function (e) {
    wx.navigateTo({
      url: '../order-details/order-details?orderId=' + e.currentTarget.id
    })
  },

  // 跳转到订单详情(小哥)
  toOrderDetails2: function (e) {
    wx.navigateTo({
      url: '../order-details2/order-details2?orderId=' + e.currentTarget.id
    })
  },
  deleteOrder: function (e) {
    var orderId = e.currentTarget.dataset.index
    let that = this
    wx.showModal({
      title: '提示',
      confirmColor: '#008cf8',
      content: '确定取消订单？',
      success: function (res) {
        if (res.confirm) {
          
          wx.request({
            url: 'https://small.qiongyazuche.com/sunday/small/order/deleteOrder',
            method: 'POST',
            header: {
              "Content-Type": "application/x-www-form-urlencoded"
            },
            data: {
              'orderId': orderId
            },
            success: function (res) {
              if (res.data.code == -1) {
                wx.showToast({
                  title: '系统异常',
                  icon: 'none',
                  duration: 1500
                })
              } else {

                that.setUserInfo()
              }

            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      pageNumber: 1
    });
    this.setUserInfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let pageNumber = this.data.pageNumber
    pageNumber = pageNumber + 1
    this.setData({
      pageNumber: pageNumber
    });
    var serviceId='';
    var memberId='';
    if(this.data.userInfo.userType==3){
      serviceId=this.data.userInfo.id
    }else{
      memberId = this.data.userInfo.id
    }
    let that = this
    wx.request({
      url: 'https://small.qiongyazuche.com/sunday/small/order/select',
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        'serviceId':serviceId,
        'memberId': memberId,
        'pageNumber': that.data.pageNumber,
        'pageSize': that.data.pageSize,
        "orderStatuses": that.data.orderStatuses
      },
      success: function (res) {
        let orders = that.data.orders.concat(res.data.result)
        that.setData({
          orders: orders
        });
      }
    })

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '琼崖租车',
      path: '/pages/index/index'
    }
  },
  setUserInfo:function(){
    let that = this
    var data = { 'memberId': app.globalData.userInfo.id }
    var promise = app.synPostData('member/findOne', data)
    promise.then(function (res) {
      that.setData({
        userInfo: res.data.result
      })
      app.globalData.userInfo = res.data.result
      if (that.data.userInfo.userType == 3) {
        that.setData({
          isMember: false
        }) 
      }else{
        that.setData({
          isMember: true
        })
      }
 
      // console.log(that.data.userInfo.userType)
      // console.log(that.data.userInfo.id)
      // console.log(that.data.pageSize)
      // console.log(that.data.pageNumber)
      that.loadOrder(that.data.userInfo.userType, that.data.userInfo.id, that.data.pageSize, that.data.pageNumber, that.data.orderStatuses)
    });
  },
  loadOrder: function (userType, memberId, pageSize, pageNumber, orderStatuses){
    let that=this
      var data;
      if(userType==3){
        data = { "serviceId": memberId, "pageSize": pageSize, "pageNumber": pageNumber, "orderStatuses": orderStatuses}
      }else{
        data = { "memberId": memberId, "pageSize": pageSize, "pageNumber": pageNumber, "orderStatuses": orderStatuses }
      }
      var promise = app.synPostData('order/select', data)

      promise.then(function (res) {

        that.setData({
          orders: res.data.result
        })
      });

  }
})